/*
 * CS_SOCIAL_FRIEND_PLAYABLE.cpp
 *
 *  Created on: 2013. 6. 4.
 *      Author: the2
 */


#include "../../Command.h"
#include "../../User.h"
#include "../../Proxy.h"
#include "../../NLogic/Space.h"
#include "../../NLogic/SocialInfo.h"

namespace ProjectZ { namespace NCommand {

int
Is_Playable(User * pTarget, int type)
{
	int playable = 0;

	DEBUGLOG("type=%d invite_block=%d", type, pTarget->GetSocialInfo()->GetData()->_invite_blocked);
	switch(type)
	{
	case 0:	//pvp
		if( (static_cast<int>(NLogic::SocialInfo::EnumInviteState::PVP_ON) & pTarget->GetSocialInfo()->GetData()->_invite_blocked)
			&& true == pTarget->EndBattle_afterEffect() )
			playable = 1;
		break;
	case 1: //던전
		if( (static_cast<int>(NLogic::SocialInfo::EnumInviteState::DUNGEON_ON) & pTarget->GetSocialInfo()->GetData()->_invite_blocked)
			&& true == pTarget->EndBattle_afterEffect() )
			playable = 1;
		break;
	case 2: //pk
		if( (static_cast<int>(NLogic::SocialInfo::EnumInviteState::BATTLE_ROYAL_ON) & pTarget->GetSocialInfo()->GetData()->_invite_blocked)
			&& true == pTarget->EndBattle_afterEffect() )
//				&& 10 <= pTarget->GetCharacterInfo().level)
			playable = 1;
		break;
	}

	return playable;
}

void Command::CS_SOCIAL_FRIEND_PLAYABLE(User * pUser, ::Network::Packet * pPacket)
{
	PREPARE_PACKET;

	uint8_t type;
	uint16_t count;
	*pPacket >> type >> count;

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	pAck->MsgCat("%2d%2d", ACK_OK, count);
	for( int i = 0 ; i < count ; i++ )
	{
		uint32_t user_seq = 0;
		*pPacket >> user_seq;

		User * pTarget = ::ProjectZ::NProxy::GetUser(user_seq);
		if(pTarget != NULL)
		{
			*pAck << user_seq
					<< static_cast<uint8_t>(pTarget->GetCharacterInfo().classtype);

			NLogic::Space * pSpace = pTarget->GetSpace();
			if(NULL == pSpace)
				* pAck << static_cast<uint8_t>(0);
			else if(User::EnumState::__NONE__ == pTarget->GetEnumState() && NLogic::Space::SpaceType::WORLD == pSpace->GetData()._spaceType)
				* pAck << static_cast<uint8_t>(Is_Playable(pTarget, type));
			else
				* pAck << static_cast<uint8_t>(0);
		}
		else
		{
			*pAck << user_seq
					<< static_cast<uint8_t>(0)
					<< static_cast<uint8_t>(0);
		}
	}

	pAck->MakePacket(CMD_SC_SOCIAL_FRIEND_PLAYABLE);
	pUser->Send(pAck);
}

} /*NCommand*/ } /*ProjectZ*/

